﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
class NameCreate
{
    public static T CreateClass<T>(string className, string nameSpace="", string assemblyName="")
    {
        try
        {
            string fullName = className;//命名空间.类型名
            if (nameSpace != "")
            {
                fullName = fullName + "." + className;
            }
            string path = fullName;
            if(assemblyName.Length != 0)
            {
                path = path + "," + assemblyName;//命名空间.类型名,程序集
            }
           
            Type o = Type.GetType(path);//加载类型
            object obj = Activator.CreateInstance(o, true);//根据类型创建实例
            return (T)obj;//类型转换并返回
        }
        catch
        {
            //发生异常，返回类型的默认值
            return default(T);
        }
    }
    public static MethodInfo CreateMethod(string methodName, Type[] paramType, string className, string nameSpace = "", string assemblyName = "")
    {
        MethodInfo ret = null;
        try
        {
            string fullName = nameSpace;//命名空间.类型名
            if (nameSpace != "")
            {
                fullName = fullName + "." + className;
            }
            else
            {
                fullName = className;
            }
            string path = fullName;
            if (assemblyName.Length != 0)
            {
                path = path + "," + assemblyName;//命名空间.类型名,程序集
            }
            Type tx = Type.GetType(path);
            if(tx != null)
            {
                if(paramType != null)
                {
                    ret = tx.GetMethod(methodName, paramType);
                }
                else
                {
                    ret = tx.GetMethod(methodName);
                }
            }
        }
        catch
        {
            //发生异常，返回类型的默认值
        }
        return ret;
    }

}

